package com.diandi.signature.utils;

import com.alibaba.fastjson.JSONArray;
import com.diandi.signature.enums.platfrom.ContractTypeAndTempName;
import com.diandi.signature.url.CreateTradeNo;
import com.diandi.signature.url.GsonUtil;
import org.apache.commons.lang3.StringUtils;

import java.util.*;

/**
 *  用于签章<根据模板类型来签章>
 * @author mwz
 * @create 2017-08-23 13:11
 * @version V1.0.0
 */
public class GenViewMapTools {

    public static Map<String, Object> genContractParamMap(String contractType, String params){
        Map<String, Object> returnMap = new HashMap<String, Object>();

        if (contractType.equals(ContractTypeAndTempName.借款协议.getType())){
            returnMap = genLoanMap(params);
            return returnMap;
        }
        if (contractType.equals(ContractTypeAndTempName.信息咨询与管理服务协议.getType())){
            returnMap = genInforServiceMap(params);
            return returnMap;
        }
        if (contractType.equals(ContractTypeAndTempName.借款人承诺书.getType())){
            returnMap = genLetterOfCommitmentMap(params);
            return returnMap;
        }
        if (contractType.equals(ContractTypeAndTempName.债券转让协议.getType())){
            returnMap = genBondTransferMap(params);
            return returnMap;
        }
        if (contractType.equals(ContractTypeAndTempName.授权委托协议.getType())){
            returnMap = genDelegationMap(params);
            return returnMap;
        }
        if (contractType.equals(ContractTypeAndTempName.委托代扣协议_循环贷.getType())){
            returnMap = genEntrustWithHoldMap(params);
            return returnMap;
        }
        if (contractType.equals(ContractTypeAndTempName.信息咨询与管理服务协议_包年服务.getType())){
            returnMap = packYear(params);
            return returnMap;
        }
        if (contractType.equals(ContractTypeAndTempName.借款协议并委托代扣协议.getType())){
            returnMap = genLoanAndEntrustWithHoldMap(params);
            return returnMap;
        }
        if (contractType.equals(ContractTypeAndTempName.委托划款授权书_投资人.getType())){
            returnMap = genLetterOfAuthorizationMap(params);
            return returnMap;
        }
        if (contractType.equals(ContractTypeAndTempName.借款协议_High购房.getType())){
            returnMap = genLoanOnlinMap(params);
            return returnMap;
        }
        if (contractType.equals(ContractTypeAndTempName.信息咨询与管理服务协议_线上.getType())){
            returnMap = genInforServiceOnLineMap(params);
            return returnMap;
        }
        if (contractType.equals(ContractTypeAndTempName.借款协议_有用分期.getType())){
            returnMap = genLoanByStagesMAp(params);
            return returnMap;
        }
        if(contractType.equals(ContractTypeAndTempName.借款协议_High酒店.getType())){
            returnMap = genLoanHighHotalMap(params);
            return returnMap;
        }
        if (contractType.equals(ContractTypeAndTempName.借款协议_High游艇.getType())){
            returnMap = genLoanHighYachtMap(params);
            return returnMap;
        }
        if (contractType.equals(ContractTypeAndTempName.借款协议_秒借.getType())){
            returnMap = genLoanMiaoJieMap(params);
            return returnMap;
        }
        if (contractType.equals(ContractTypeAndTempName.借款协议_苹果专享宝.getType())){
            returnMap = genLoanAppleMap(params);
            return returnMap;
        }
        if (contractType.equals(ContractTypeAndTempName.借款协议_车贷宝.getType())){
            returnMap = genLoanCurrencyMap(params);
            return returnMap;
        }
        if (contractType.equals(ContractTypeAndTempName.借款协议_企业机构.getType())){
            returnMap = genLoanEnterMap(params);
            return returnMap;
        }
        if(contractType.equals(ContractTypeAndTempName.委托代偿协议.getType())){
            returnMap = genLoanServiceMap(params);
            return returnMap;
        }
        if(contractType.equals(ContractTypeAndTempName.定期宝服务协议.getType())){
            returnMap = genRegularTreasureServiceAgreementMap(params);
            return returnMap;
        }
        if(contractType.equals(ContractTypeAndTempName.活期宝服务协议.getType())){
            returnMap = genCurrentTreasureServiceAgreementMap(params);
            return returnMap;
        }
        if(contractType.equals(ContractTypeAndTempName.电子签章使用授权书.getType())){
            returnMap = genQZUseAuthorizationMap(params);
            return returnMap;
        }
        if(contractType.equals(ContractTypeAndTempName.资金出借风险提示书.getType())){
            returnMap = genRiskNoteMap(params);
            return returnMap;
        }
        if(contractType.equals(ContractTypeAndTempName.车信宝借款协议.getType())){
            returnMap = genCheXinBaoMap(params);
            return returnMap;
        }
        if(contractType.equals(ContractTypeAndTempName.定期宝借款协议.getType())){
            returnMap = genRegularLoanMap(params);
            return returnMap;
        }
        if (contractType.equals(ContractTypeAndTempName.活期宝.getType())) {
            returnMap = genHuoQiBaoLoanMap(params);
            return returnMap;
        }
        if (contractType.equals(ContractTypeAndTempName.包年服务借款业务规则.getType())){
            returnMap = genLoanBusinessRules(params);
            return returnMap;
        }


        returnMap.put("responseCode", "QZ_7007");
        returnMap.put("responseMsg", "合同类型不合法");

        return returnMap;
    }

    private static Map genHuoQiBaoLoanMap(String params) {
        Map returnMap = null;
        Map paramMap = GsonUtil.fronJson2Map(params);

        if (returnMap != null && !returnMap.isEmpty()){
            return returnMap;
        }

        if (!paramMap.containsKey("contractNo") || StringUtils.isBlank(paramMap.get("contractNo").toString())){
            paramMap.put("contractNo", CreateTradeNo.createTradeNo("M26"));
        }
        return paramMap;
    }

    private static Map<String,Object> genRegularLoanMap(String params) {
        Map<String, String> returnMap = null;
        Map<String, String> paramMap = GsonUtil.fronJson2Map(params);
        Map<String, Object> paramMapObj = new HashMap<String, Object>();


        JSONArray lenderInfoArray = JSONArray.parseArray(paramMap.get("lenderInfoList"));
        List<Map<String, String>> lenderInfoList = new ArrayList<Map<String, String>>();
        Map<String, String> lenderInfoMap = null;
        if (lenderInfoArray != null && !lenderInfoArray.isEmpty()){
            for (Object lenderInfo : lenderInfoArray) {
                lenderInfoMap = GsonUtil.fronJson2Map(lenderInfo.toString());
                lenderInfoList.add(lenderInfoMap);
            }
        }

        JSONArray repayDetailArray = JSONArray.parseArray(paramMap.get("repayDetailList"));
        List<Map<String, String>> repayDetailList = new ArrayList<Map<String, String>>();
        Map<String, String> repayDetailMap = null;
        if (repayDetailArray != null && !repayDetailArray.isEmpty()){
            for (Object repayDetail : repayDetailArray) {
                repayDetailMap = GsonUtil.fronJson2Map(repayDetail.toString());
                repayDetailList.add(repayDetailMap);
            }
        }

        Set<String> paramKeys = paramMap.keySet();
        for (String key : paramKeys) {
            paramMapObj.put(key, paramMap.get(key));
        }

        if (!paramMap.containsKey("contractNo") || StringUtils.isBlank(paramMap.get("contractNo").toString())){
            paramMapObj.put("contractNo", CreateTradeNo.createTradeNo("M25"));
        }

        paramMapObj.put("lenderInfoList", lenderInfoList);
        paramMapObj.put("repayDetailList", repayDetailList);

        return paramMapObj;
    }

    /**
     * 生成车信宝借款协议参数
     * @param params
     * @return
     */
    private static Map<String,Object> genCheXinBaoMap(String params) {
        Map<String, String> returnMap = null;
        Map<String, String> paramMap = GsonUtil.fronJson2Map(params);
        Map<String, Object> paramMapObj = new HashMap<String, Object>();


        JSONArray lenderInfoArray = JSONArray.parseArray(paramMap.get("lenderInfoList"));
        List<Map<String, String>> lenderInfoList = new ArrayList<Map<String, String>>();
        Map<String, String> lenderInfoMap = null;
        if (lenderInfoArray != null && !lenderInfoArray.isEmpty()){
            for (Object lenderInfo : lenderInfoArray) {
                lenderInfoMap = GsonUtil.fronJson2Map(lenderInfo.toString());
                lenderInfoList.add(lenderInfoMap);
            }
        }

        JSONArray repayDetailArray = JSONArray.parseArray(paramMap.get("repayDetailList"));
        List<Map<String, String>> repayDetailList = new ArrayList<Map<String, String>>();
        Map<String, String> repayDetailMap = null;
        if (repayDetailArray != null && !repayDetailArray.isEmpty()){
            for (Object repayDetail : repayDetailArray) {
                repayDetailMap = GsonUtil.fronJson2Map(repayDetail.toString());
                repayDetailList.add(repayDetailMap);
            }
        }

        Set<String> paramKeys = paramMap.keySet();
        for (String key : paramKeys) {
            paramMapObj.put(key, paramMap.get(key));
        }

        if (!paramMap.containsKey("contractNo") || StringUtils.isBlank(paramMap.get("contractNo").toString())){
            paramMapObj.put("contractNo", CreateTradeNo.createTradeNo("M24"));
        }

        paramMapObj.put("lenderInfoList", lenderInfoList);
        paramMapObj.put("repayDetailList", repayDetailList);

        return paramMapObj;
    }


    /**
     * 生成资金出借风险提示书参数
     * @param params
     * @return
     */
    private static Map<String,Object> genRiskNoteMap(String params) {
        Map returnMap = null;
        Map paramMap = GsonUtil.fronJson2Map(params);

        if (returnMap != null && !returnMap.isEmpty()){
            return returnMap;
        }

        if (!paramMap.containsKey("contractNo") || StringUtils.isBlank(paramMap.get("contractNo").toString())){
            paramMap.put("contractNo", CreateTradeNo.createTradeNo("M23"));
        }
        return paramMap;
    }

    /**
     * 生成电子签章使用授权书参数
     * @param params
     * @return
     */
    private static Map<String,Object> genQZUseAuthorizationMap(String params) {
        Map returnMap = null;
        Map paramMap = GsonUtil.fronJson2Map(params);

        if (returnMap != null && !returnMap.isEmpty()){
            return returnMap;
        }

        if (!paramMap.containsKey("contractNo") || StringUtils.isBlank(paramMap.get("contractNo").toString())){
            paramMap.put("contractNo", CreateTradeNo.createTradeNo("M22"));
        }
        return paramMap;
    }

    /**
     * 生成活期宝服务协议参数
     * @param params
     * @return
     */
    private static Map genCurrentTreasureServiceAgreementMap(String params) {
        Map returnMap = null;
        Map paramMap = GsonUtil.fronJson2Map(params);

        if (returnMap != null && !returnMap.isEmpty()){
            return returnMap;
        }

        if (!paramMap.containsKey("contractNo") || StringUtils.isBlank(paramMap.get("contractNo").toString())){
            paramMap.put("contractNo", CreateTradeNo.createTradeNo("M20"));
        }
        return paramMap;
    }

    /**
     * 生成定期宝服务协议参数
     * @param params
     * @return
     */
    private static Map genRegularTreasureServiceAgreementMap(String params) {
        Map returnMap = null;
        Map paramMap = GsonUtil.fronJson2Map(params);

        if (returnMap != null && !returnMap.isEmpty()){
            return returnMap;
        }

        if (!paramMap.containsKey("contractNo") || StringUtils.isBlank(paramMap.get("contractNo").toString())){
            paramMap.put("contractNo", CreateTradeNo.createTradeNo("M19"));
        }
        return paramMap;
    }

    /**
     * 生成借款协议和信息咨询与管理服务协议参数
     * @param params
     * @return
     */
    private static Map genLoanServiceMap(String params) {

        Map returnMap = null;
        Map paramMap = GsonUtil.fronJson2Map(params);

        if (returnMap != null && !returnMap.isEmpty()){
            return returnMap;
        }

        if (!paramMap.containsKey("contractNo") || StringUtils.isBlank(paramMap.get("contractNo").toString())){
            paramMap.put("contractNo", CreateTradeNo.createTradeNo("M21"));
        }
        return paramMap;
    }

    /**
     * 生成借款协议_企业机构参数
     * @param params
     * @return
     */
    private static Map genLoanEnterMap(String params) {
        Map<String, String> returnMap = null;
        Map<String, String> paramMap = GsonUtil.fronJson2Map(params);
        Map<String, Object> paramMapObj = new HashMap<String, Object>();


        JSONArray lenderInfoArray = JSONArray.parseArray(paramMap.get("lenderInfoList"));
        List<Map<String, String>> lenderInfoList = new ArrayList<Map<String, String>>();
        Map<String, String> lenderInfoMap = null;
        if (lenderInfoArray != null && !lenderInfoArray.isEmpty()){
            for (Object lenderInfo : lenderInfoArray) {
                lenderInfoMap = GsonUtil.fronJson2Map(lenderInfo.toString());
                lenderInfoList.add(lenderInfoMap);
            }
        }

        JSONArray repayDetailArray = JSONArray.parseArray(paramMap.get("repayDetailList"));
        List<Map<String, String>> repayDetailList = new ArrayList<Map<String, String>>();
        Map<String, String> repayDetailMap = null;
        if (repayDetailArray != null && !repayDetailArray.isEmpty()){
            for (Object repayDetail : repayDetailArray) {
                repayDetailMap = GsonUtil.fronJson2Map(repayDetail.toString());
                repayDetailList.add(repayDetailMap);
            }
        }

        Set<String> paramKeys = paramMap.keySet();
        for (String key : paramKeys) {
            paramMapObj.put(key, paramMap.get(key));
        }

        if (!paramMap.containsKey("contractNo") || StringUtils.isBlank(paramMap.get("contractNo").toString())){
            paramMapObj.put("contractNo", CreateTradeNo.createTradeNo("M18"));
        }

        paramMapObj.put("lenderInfoList", lenderInfoList);
        paramMapObj.put("repayDetailList", repayDetailList);

        return paramMapObj;
    }


    /**
     * 生成借款协议_车贷宝参数
     * @param params
     * @return
     */
    private static Map genLoanCurrencyMap(String params) {
        Map<String, String> returnMap = null;
        Map<String, String> paramMap = GsonUtil.fronJson2Map(params);
        Map<String, Object> paramMapObj = new HashMap<String, Object>();

        JSONArray lenderInfoArray = JSONArray.parseArray(paramMap.get("lenderInfoList"));
        List<Map<String, String>> lenderInfoList = new ArrayList<Map<String, String>>();
        Map<String, String> lenderInfoMap = null;
        if (lenderInfoArray != null && !lenderInfoArray.isEmpty()){
            for (Object lenderInfo : lenderInfoArray) {
                lenderInfoMap = GsonUtil.fronJson2Map(lenderInfo.toString());
                lenderInfoList.add(lenderInfoMap);
            }
        }

        JSONArray repayDetailArray = JSONArray.parseArray(paramMap.get("repayDetailList"));
        List<Map<String, String>> repayDetailList = new ArrayList<Map<String, String>>();
        Map<String, String> repayDetailMap = null;
        if (repayDetailArray != null && !repayDetailArray.isEmpty()){
            for (Object repayDetail : repayDetailArray) {
                repayDetailMap = GsonUtil.fronJson2Map(repayDetail.toString());
                repayDetailList.add(repayDetailMap);
            }
        }

        Set<String> paramKeys = paramMap.keySet();
        for (String key : paramKeys) {
            paramMapObj.put(key, paramMap.get(key));
        }

        if (!paramMap.containsKey("contractNo") || StringUtils.isBlank(paramMap.get("contractNo").toString())){
            paramMapObj.put("contractNo", CreateTradeNo.createTradeNo("M17"));
        }

        paramMapObj.put("lenderInfoList", lenderInfoList);
        paramMapObj.put("repayDetailList", repayDetailList);

        return paramMapObj;
    }

    /**
     * 生成借款协议_苹果专享宝参数
     * @param params
     * @return
     */
    private static Map genLoanAppleMap(String params) {
        Map<String, String> returnMap = null;
        Map<String, String> paramMap = GsonUtil.fronJson2Map(params);
        Map<String, Object> paramMapObj = new HashMap<String, Object>();


        JSONArray lenderInfoArray = JSONArray.parseArray(paramMap.get("lenderInfoList"));
        List<Map<String, String>> lenderInfoList = new ArrayList<Map<String, String>>();
        Map<String, String> lenderInfoMap = null;
        if (lenderInfoArray != null && !lenderInfoArray.isEmpty()){
            for (Object lenderInfo : lenderInfoArray) {
                lenderInfoMap = GsonUtil.fronJson2Map(lenderInfo.toString());
                lenderInfoList.add(lenderInfoMap);
            }
        }

        JSONArray repayDetailArray = JSONArray.parseArray(paramMap.get("repayDetailList"));
        List<Map<String, String>> repayDetailList = new ArrayList<Map<String, String>>();
        Map<String, String> repayDetailMap = null;
        if (repayDetailArray != null && !repayDetailArray.isEmpty()){
            for (Object repayDetail : repayDetailArray) {
                repayDetailMap = GsonUtil.fronJson2Map(repayDetail.toString());
                repayDetailList.add(repayDetailMap);
            }
        }

        Set<String> paramKeys = paramMap.keySet();
        for (String key : paramKeys) {
            paramMapObj.put(key, paramMap.get(key));
        }

        if (!paramMap.containsKey("contractNo") || StringUtils.isBlank(paramMap.get("contractNo").toString())){
            paramMapObj.put("contractNo", CreateTradeNo.createTradeNo("M16"));
        }

        paramMapObj.put("lenderInfoList", lenderInfoList);
        paramMapObj.put("repayDetailList", repayDetailList);

        return paramMapObj;
    }


    /**
     * 生成借款协议_秒借参数
     * @param params
     * @return
     */
    private static Map genLoanMiaoJieMap(String params) {
        Map<String, String> returnMap = null;
        Map<String, String> paramMap = GsonUtil.fronJson2Map(params);
        Map<String, Object> paramMapObj = new HashMap<String, Object>();

        if (returnMap != null && !returnMap.isEmpty()){
            return returnMap;
        }

        JSONArray lenderInfoArray = JSONArray.parseArray(paramMap.get("lenderInfoList"));
        List<Map<String, String>> lenderInfoList = new ArrayList<Map<String, String>>();
        Map<String, String> lenderInfoMap = null;
        if (lenderInfoArray != null && !lenderInfoArray.isEmpty()){
            for (Object lenderInfo : lenderInfoArray) {
                lenderInfoMap = GsonUtil.fronJson2Map(lenderInfo.toString());
                lenderInfoList.add(lenderInfoMap);
            }
        }

        JSONArray repayDetailArray = JSONArray.parseArray(paramMap.get("repayDetailList"));
        List<Map<String, String>> repayDetailList = new ArrayList<Map<String, String>>();
        Map<String, String> repayDetailMap = null;
        if (repayDetailArray != null && !repayDetailArray.isEmpty()){
            for (Object repayDetail : repayDetailArray) {
                repayDetailMap = GsonUtil.fronJson2Map(repayDetail.toString());
                repayDetailList.add(repayDetailMap);
            }
        }

        Set<String> paramKeys = paramMap.keySet();
        for (String key : paramKeys) {
            paramMapObj.put(key, paramMap.get(key));
        }

        if (!paramMap.containsKey("contractNo") || StringUtils.isBlank(paramMap.get("contractNo").toString())){
            paramMapObj.put("contractNo", CreateTradeNo.createTradeNo("M15"));
        }

        paramMapObj.put("lenderInfoList", lenderInfoList);
        paramMapObj.put("repayDetailList", repayDetailList);

        return paramMapObj;
    }


    /**
     * 生成借款协议_High游艇参数
     * @param params
     * @return
     */
    private static Map genLoanHighYachtMap(String params) {
        Map<String, String> returnMap = null;
        Map<String, String> paramMap = GsonUtil.fronJson2Map(params);
        Map<String, Object> paramMapObj = new HashMap<String, Object>();

        if (returnMap != null && !returnMap.isEmpty()){
            return returnMap;
        }

        JSONArray lenderInfoArray = JSONArray.parseArray(paramMap.get("lenderInfoList"));
        List<Map<String, String>> lenderInfoList = new ArrayList<Map<String, String>>();
        Map<String, String> lenderInfoMap = null;
        if (lenderInfoArray != null && !lenderInfoArray.isEmpty()){
            for (Object lenderInfo : lenderInfoArray) {
                lenderInfoMap = GsonUtil.fronJson2Map(lenderInfo.toString());
                lenderInfoList.add(lenderInfoMap);
            }
        }

        JSONArray repayDetailArray = JSONArray.parseArray(paramMap.get("repayDetailList"));
        List<Map<String, String>> repayDetailList = new ArrayList<Map<String, String>>();
        Map<String, String> repayDetailMap = null;
        if (repayDetailArray != null && !repayDetailArray.isEmpty()){
            for (Object repayDetail : repayDetailArray) {
                repayDetailMap = GsonUtil.fronJson2Map(repayDetail.toString());
                repayDetailList.add(repayDetailMap);
            }
        }

        Set<String> paramKeys = paramMap.keySet();
        for (String key : paramKeys) {
            paramMapObj.put(key, paramMap.get(key));
        }

        if (!paramMap.containsKey("contractNo") || StringUtils.isBlank(paramMap.get("contractNo").toString())){
            paramMapObj.put("contractNo", CreateTradeNo.createTradeNo("M14"));
        }

        paramMapObj.put("lenderInfoList", lenderInfoList);
        paramMapObj.put("repayDetailList", repayDetailList);

        return paramMapObj;
    }

    /**
     * 生成借款协议_High酒店参数
     * @param params
     * @return
     */
    private static Map genLoanHighHotalMap(String params) {
        Map<String, String> returnMap = null;
        Map<String, String> paramMap = GsonUtil.fronJson2Map(params);
        Map<String, Object> paramMapObj = new HashMap<String, Object>();

        if (returnMap != null && !returnMap.isEmpty()){
            return returnMap;
        }

        JSONArray lenderInfoArray = JSONArray.parseArray(paramMap.get("lenderInfoList"));
        List<Map<String, String>> lenderInfoList = new ArrayList<Map<String, String>>();
        Map<String, String> lenderInfoMap = null;
        if (lenderInfoArray != null && !lenderInfoArray.isEmpty()){
            for (Object lenderInfo : lenderInfoArray) {
                lenderInfoMap = GsonUtil.fronJson2Map(lenderInfo.toString());
                lenderInfoList.add(lenderInfoMap);
            }
        }

        JSONArray repayDetailArray = JSONArray.parseArray(paramMap.get("repayDetailList"));
        List<Map<String, String>> repayDetailList = new ArrayList<Map<String, String>>();
        Map<String, String> repayDetailMap = null;
        if (repayDetailArray != null && !repayDetailArray.isEmpty()){
            for (Object repayDetail : repayDetailArray) {
                repayDetailMap = GsonUtil.fronJson2Map(repayDetail.toString());
                repayDetailList.add(repayDetailMap);
            }
        }

        Set<String> paramKeys = paramMap.keySet();
        for (String key : paramKeys) {
            paramMapObj.put(key, paramMap.get(key));
        }

        if (!paramMap.containsKey("contractNo") || StringUtils.isBlank(paramMap.get("contractNo").toString())){
            paramMapObj.put("contractNo", CreateTradeNo.createTradeNo("M13"));
        }

        paramMapObj.put("lenderInfoList", lenderInfoList);
        paramMapObj.put("repayDetailList", repayDetailList);

        return paramMapObj;
    }

    /**
     * 生成借款协议有用分期参数
     * @param params
     * @return
     */
    private static Map genLoanByStagesMAp(String params) {
        Map<String, String> returnMap = null;
        Map<String, String> paramMap = GsonUtil.fronJson2Map(params);
        Map<String, Object> paramMapObj = new HashMap<String, Object>();


        if (returnMap != null && !returnMap.isEmpty()){
            return returnMap;
        }

        JSONArray lenderInfoArray = JSONArray.parseArray(paramMap.get("lenderInfoList"));
        List<Map<String, String>> lenderInfoList = new ArrayList<Map<String, String>>();
        Map<String, String> lenderInfoMap = null;
        if (lenderInfoArray != null && !lenderInfoArray.isEmpty()){
            for (Object lenderInfo : lenderInfoArray) {
                lenderInfoMap = GsonUtil.fronJson2Map(lenderInfo.toString());
                lenderInfoList.add(lenderInfoMap);
            }
        }

        JSONArray repayDetailArray = JSONArray.parseArray(paramMap.get("repayDetailList"));
        List<Map<String, String>> repayDetailList = new ArrayList<Map<String, String>>();
        Map<String, String> repayDetailMap = null;
        if (repayDetailArray != null && !repayDetailArray.isEmpty()){
            for (Object repayDetail : repayDetailArray) {
                repayDetailMap = GsonUtil.fronJson2Map(repayDetail.toString());
                repayDetailList.add(repayDetailMap);
            }
        }

        Set<String> paramKeys = paramMap.keySet();
        for (String key : paramKeys) {
            paramMapObj.put(key, paramMap.get(key));
        }

        if (!paramMap.containsKey("contractNo") || StringUtils.isBlank(paramMap.get("contractNo").toString())){
            paramMapObj.put("contractNo", CreateTradeNo.createTradeNo("M12"));
        }

        paramMapObj.put("lenderInfoList", lenderInfoList);
        paramMapObj.put("repayDetailList", repayDetailList);

        return paramMapObj;
    }

    /**
     * 生成 信息咨询与管理服务协议_线上参数
     * @param params
     * @return
     */
    private static Map genInforServiceOnLineMap(String params) {
        Map returnMap = null;
        Map paramMap = GsonUtil.fronJson2Map(params);

        if (returnMap != null && !returnMap.isEmpty()){
            return returnMap;
        }

        if (!paramMap.containsKey("contractNo") || StringUtils.isBlank(paramMap.get("contractNo").toString())){
            paramMap.put("contractNo", CreateTradeNo.createTradeNo("M11"));
        }
        return paramMap;
    }

    /**
     * 生成借款协议_High购房参数
     * @param params
     * @return
     */
    private static Map genLoanOnlinMap(String params) {

        Map<String, String> returnMap = null;
        Map<String, String> paramMap = GsonUtil.fronJson2Map(params);
        Map<String, Object> paramMapObj = new HashMap<String, Object>();


        if (returnMap != null && !returnMap.isEmpty()){
            return returnMap;
        }

        JSONArray lenderInfoArray = JSONArray.parseArray(paramMap.get("lenderInfoList"));
        List<Map<String, String>> lenderInfoList = new ArrayList<Map<String, String>>();
        Map<String, String> lenderInfoMap = null;
        if (lenderInfoArray != null && !lenderInfoArray.isEmpty()){
            for (Object lenderInfo : lenderInfoArray) {
                lenderInfoMap = GsonUtil.fronJson2Map(lenderInfo.toString());
                lenderInfoList.add(lenderInfoMap);
            }
        }

        JSONArray repayDetailArray = JSONArray.parseArray(paramMap.get("repayDetailList"));
        List<Map<String, String>> repayDetailList = new ArrayList<Map<String, String>>();
        Map<String, String> repayDetailMap = null;
        if (repayDetailArray != null && !repayDetailArray.isEmpty()){
            for (Object repayDetail : repayDetailArray) {
                repayDetailMap = GsonUtil.fronJson2Map(repayDetail.toString());
                repayDetailList.add(repayDetailMap);
            }
        }

        Set<String> paramKeys = paramMap.keySet();
        for (String key : paramKeys) {
            paramMapObj.put(key, paramMap.get(key));
        }

        if (!paramMap.containsKey("contractNo") || StringUtils.isBlank(paramMap.get("contractNo").toString())){
            paramMapObj.put("contractNo", CreateTradeNo.createTradeNo("M10"));
        }

        paramMapObj.put("lenderInfoList", lenderInfoList);
        paramMapObj.put("repayDetailList", repayDetailList);

        return paramMapObj;
    }

    /**
     * 生成委托划款授权书_投资人参数
     * @param params
     * @return
     */
    private static Map<String,Object> genLetterOfAuthorizationMap(String params) {
        Map returnMap = new HashMap();
        Map paramMap = GsonUtil.fronJson2Map(params);


        if (returnMap != null && !returnMap.isEmpty()){
            return returnMap;
        }

        if (!paramMap.containsKey("contractNo") || StringUtils.isBlank(paramMap.get("contractNo").toString())){
            paramMap.put("contractNo", CreateTradeNo.createTradeNo("M9"));
        }

        return paramMap;
    }

    /**
     * 生成债券转让协议参数
     * @param params
     * @return
     */
    public static Map genBondTransferMap(String params){
        Map returnMap = null;

        Map<String, Object> paramMapObj = new HashMap<String, Object>();
        Map<String, String> paramMap = GsonUtil.fronJson2Map(params);

        if (returnMap != null && !returnMap.isEmpty()){
            return returnMap;
        }

        JSONArray assignorArray = JSONArray.parseArray(paramMap.get("assignorList"));
        List<Map<String, String>> assignorList = new ArrayList<Map<String, String>>();

        if(assignorArray != null && !assignorArray.isEmpty()){
        for (Object assignor : assignorArray) {
            Map assignorMap = GsonUtil.fronJson2Map(assignor.toString());

            assignorList.add(assignorMap);
            }
        }

        Set<String> keys = paramMap.keySet();
        for (String key : keys) {
            paramMapObj.put(key, paramMap.get(key));
        }

        if (!paramMapObj.containsKey("contractNo") || StringUtils.isBlank(paramMapObj.get("contractNo").toString())){
            paramMapObj.put("contractNo", CreateTradeNo.createTradeNo("M4"));
        }

        paramMapObj.put("assignorList", assignorList);

        return paramMapObj;
    }

    /**
     * 生成授权委托协议参数
     * @param params
     * @return
     */
    public static Map genDelegationMap(String params){
        Map returnMap = new HashMap();
        Map paramMap = GsonUtil.fronJson2Map(params);

        if (returnMap != null && !returnMap.isEmpty()){
            return returnMap;
        }

        if (!paramMap.containsKey("contractNo") || StringUtils.isBlank(paramMap.get("contractNo").toString())){
            paramMap.put("contractNo", CreateTradeNo.createTradeNo("M5"));
        }

        return paramMap;
    }

    /**
     * 生成委托代扣协议参数
     * @param params
     * @return
     */
    public static Map genEntrustWithHoldMap(String params){
        Map returnMap = new HashMap();
        Map paramMap = GsonUtil.fronJson2Map(params);

        if (returnMap != null && !returnMap.isEmpty()){
            return returnMap;
        }
        if (!paramMap.containsKey("contractNo") || StringUtils.isBlank(paramMap.get("contractNo").toString())){
            paramMap.put("contractNo", CreateTradeNo.createTradeNo("M6"));
        }

        return paramMap;
    }

    /**
     * 生成信息咨询与管理服务协议参数
     * @param params
     * @return
     */
    public static Map genInforServiceMap(String params){
        Map returnMap = null;
        Map paramMap = GsonUtil.fronJson2Map(params);

        if (returnMap != null && !returnMap.isEmpty()){
            return returnMap;
        }

        if (!paramMap.containsKey("contractNo") || StringUtils.isBlank(paramMap.get("contractNo").toString())){
            paramMap.put("contractNo", CreateTradeNo.createTradeNo("M2"));
        }
        return paramMap;
    }

    /**
     * 生成借款人承诺书参数
     * @param params
     * @return
     */
    public static Map genLetterOfCommitmentMap(String params){
        Map returnMap = null;
        Map paramMap = GsonUtil.fronJson2Map(params);

        if (returnMap != null && !returnMap.isEmpty()){
            return returnMap;
        }

        if (!paramMap.containsKey("contractNo") || StringUtils.isBlank(paramMap.get("contractNo").toString())){
            paramMap.put("contractNo", CreateTradeNo.createTradeNo("M3"));
        }

        return paramMap;
    }


    /**
     * 生成借款协议参数
     * @param params
     * @return
     */
    public static Map genLoanMap(String params){
        Map<String, String> returnMap = null;
        Map<String, String> paramMap = GsonUtil.fronJson2Map(params);
        Map<String, Object> paramMapObj = new HashMap<String, Object>();


        JSONArray lenderInfoArray = JSONArray.parseArray(paramMap.get("lenderInfoList"));
        List<Map<String, String>> lenderInfoList = new ArrayList<Map<String, String>>();
        Map<String, String> lenderInfoMap = null;
        if (lenderInfoArray != null && !lenderInfoArray.isEmpty()){
            for (Object lenderInfo : lenderInfoArray) {
                lenderInfoMap = GsonUtil.fronJson2Map(lenderInfo.toString());
                lenderInfoList.add(lenderInfoMap);
            }
        }

        JSONArray repayDetailArray = JSONArray.parseArray(paramMap.get("repayDetailList"));
        List<Map<String, String>> repayDetailList = new ArrayList<Map<String, String>>();
        Map<String, String> repayDetailMap = null;
        if (repayDetailArray != null && !repayDetailArray.isEmpty()){
            for (Object repayDetail : repayDetailArray) {
                repayDetailMap = GsonUtil.fronJson2Map(repayDetail.toString());
                repayDetailList.add(repayDetailMap);
            }
        }

        Set<String> paramKeys = paramMap.keySet();
        for (String key : paramKeys) {
            paramMapObj.put(key, paramMap.get(key));
        }

        if (!paramMap.containsKey("contractNo") || StringUtils.isBlank(paramMap.get("contractNo").toString())){
            paramMapObj.put("contractNo", CreateTradeNo.createTradeNo("M1"));
        }

        paramMapObj.put("lenderInfoList", lenderInfoList);
        paramMapObj.put("repayDetailList", repayDetailList);

        return paramMapObj;
    }

    public static Map packYear(String params){
        Map<String, String> returnMap = null;
        Map paramMap = GsonUtil.fronJson2Map(params);

        if (returnMap != null && !returnMap.isEmpty()){
            return returnMap;
        }

        if (!paramMap.containsKey("contractNo") || StringUtils.isBlank(paramMap.get("contractNo").toString())){
            paramMap.put("contractNo", CreateTradeNo.createTradeNo("M7"));
        }
        return paramMap;
    }

    public static Map genLoanAndEntrustWithHoldMap(String params){
        Map genEntrustWithHoldMap = genEntrustWithHoldMap(params);
        if (genEntrustWithHoldMap.containsKey("responseCode")){
            return genEntrustWithHoldMap;
        }
        Map genLoanMap = genLoanMap(params);

        return genLoanMap;
    }

    public static Map genLoanBusinessRules(String params){
        Map paramMap = GsonUtil.fronJson2Map(params);
        if (!paramMap.containsKey("contractNo") || StringUtils.isBlank(paramMap.get("contractNo").toString())){
            paramMap.put("contractNo", CreateTradeNo.createTradeNo("M27"));
        }
        return paramMap;
    }
}
